package com.ccn.crm.taskController;

import com.ccn.crm.interfaces.impl.*;

import com.ccn.crm.mapper.MiddleTableMapper;
import com.ccn.crm.model.MiddleTable;
import com.ccn.crm.service.IAfterSales;
import com.ccn.crm.util.EmailUtil;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Component
public class taskController  {
    @Autowired
    private IPushData pushData;
    @Autowired
    private IMail iMail;
    @Autowired
    private IPushInstall iPushInstall;
    @Autowired
    private IGetAfterSales iGetAfterSales;
    @Autowired
    private IEquipmentEmail iEquipmentEmail;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private MiddleTableMapper middleTableMapper;
    private static final Logger LOG = LoggerFactory.getLogger(taskController.class);
    //每15分钟
//    @Scheduled(cron="0 */2 * * * ?")
    @Scheduled(cron="0 */15 * * * ?")
    public void run() {
        try {
            LOG.info("=============获取新建项目================");
            pushData.getData();
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e );
//                LOG.error("error: {}", e.getMessage(), e);
                emailUtil.sendMailMe("新建项目报错---空指针异常");
            }else {
                e.printStackTrace();
                emailUtil.sendMail("新建项目报错---" + e.toString());
                Date date = new Date();
                LOG.info("运行错误信息是：" + e.toString() + "时间：" + date);
            }
        }

    }
    //每三小时
//    @Scheduled(cron="0 */2 * * * ?")
//    @Scheduled(cron="0 34 16 ? * MON-FRI")
    @Scheduled(cron="0 0 0/3 * * ?")
    public void task() {
        try {
            LOG.info("==============向crm推送数据===============");
            pushData.pushData();
            LOG.info("======================================================");
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e  );
                emailUtil.sendMailMe("向crm推送数据报错---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("向crm推送数据报错---" + e.toString());
                LOG.info("运行错误信息是：" + e + "时间：" + date);
            }
        }

    }
    //1每小时
//    @Scheduled(cron="0 */2 * * * ?")
    @Scheduled(cron="0 0 0/1 * * ?")
    public void runs() {
        try {
            LOG.info("==============更改禅道数据===============");
            pushData.getUpdateData();

        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                LOG.info("运行错误信息是：" + e  );
                e.printStackTrace();
                emailUtil.sendMailMe("更改禅道数据报错---空指针异常"+e.toString());
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("更改禅道数据报错---" + e.toString());
                LOG.info("运行错误信息是：" + e + "时间：" + date);
            }
        }

    }

    //周一到周五 9:5
//    @Scheduled(cron="0 */2 * * * ?")
@Scheduled(cron="0 5 9 ? * MON-FRI")
    public void email() {
    Date date = new Date();
    try {
        LOG.info("==============每日工时邮件发送===============");
        iMail.emailSend();
        LOG.info("邮件发送成功了时间："+date);
    } catch (Exception e) {
        if(e.toString().equals("java.lang.NullPointerException")){
            e.printStackTrace();
            LOG.info("运行错误信息是：" + e  );
            emailUtil.sendMailMe("每日发送邮件报错---空指针异常" );
        }else {
            e.printStackTrace();
            emailUtil.sendMail("每日发送邮件报错" + e.toString());
            LOG.info("运行错误信息是：" + e.toString() + "时间：" + date);
        }
    }

    }
//    21:19
//  @Scheduled(cron="0 */2 * * * ?")
  @Scheduled(cron="0 0 0/4 * * ?")
    public void workTime() {

        try {
            LOG.info("==============获取售后工时===============");
            Date date = new Date();
            iGetAfterSales.getWorkTime();
            LOG.info("获取售后工时完成："+date);
//            emailUtil.sendMail("获取售后工时成功"+date.toString());
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e  );
                emailUtil.sendMailMe("获取售后工时报错---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("获取售后工时报错" + e.toString());
                LOG.info("获取售后工时运行错误信息是：" + e + "时间：" + date);
            }
        }

    }

    //17:5
    @Scheduled(cron="0 0 10 ? * MON-FRI")
//    @Scheduled(cron="0 7 9 ? * MON-FRI")
//    @Scheduled(cron="0 */2 * * * ?")
    public void equipmentEmail() {

        try {
            LOG.info("==============发送设备未归还邮件===============");
            Date date = new Date();
            iEquipmentEmail.sentEquipmentEmail();
            LOG.info("发送设备未归还邮件："+date);
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e  );
                emailUtil.sendMailMe("发送设备未归还邮件异常---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("发送设备未归还邮件异常--" + e.toString());
                LOG.info("运行错误信息是：" + e + "时间：" + date);
            }
        }

    }

    //9:10
    @Scheduled(cron="0 20 1 ? * MON-FRI")
//    @Scheduled(cron="0 */2 * * * ?")
    public void pushInstallDetail() {
        try {
            LOG.info("==============推送实施明细===============");
            Date date = new Date();
            iPushInstall.pushDate();
//            emailUtil.sendMail("推送实施明细成功："+date.toString());
            LOG.info("推送实施明细成功："+date);
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e );
                emailUtil.sendMailMe("推送实施明细失败---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("推送实施明细失败，报错信息：" + e.toString());
                LOG.info("推送实施明细运行错误信息是：" + e + "时间：" + date);
            }
        }
    }


//        @Scheduled(cron="0 20 9 ? * ?")
//        @Scheduled(cron="0 */2 * * * ?")
    //9:10
    @Scheduled(cron="0 10 9 ? * ?")
    public void projectStatusRemind() throws Exception {

        try {
            LOG.info("==============发送内审资料整理===============");
            Date date = new Date();
            LOG.info("发送内审资料整理：" + date);
            iMail.projectStatusRemind();
            LOG.info("发送内审资料OK：" + date);
        } catch (Exception e) {
            if (e.toString().equals("java.lang.NullPointerException")) {
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e );
                emailUtil.sendMailMe("发送内审服务错误---空指针异常" );
            } else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("发送内审服务错误---" + e.toString());
                LOG.info("运行错误信息是：" + e + "时间：" + date);

            }
        }
    }

//    @Scheduled(cron="0 */2 * * * ?")
  @Scheduled(cron="0 0 0/4 * * ?")
    public void SqworkTime() {

        try {
            LOG.info("==============获取售前工时===============");
            Date date = new Date();
            iGetAfterSales.getSqWorkTime();
            LOG.info("获取售前工时："+date);
//            emailUtil.sendMail("获取售后工时成功"+date.toString());
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e  );
                emailUtil.sendMailMe("获取售前工时报错---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("获取售前工时报错" + e.toString());
                LOG.info("获取售前工时运行错误信息是：" + e + "时间：" + date);
            }
        }

    }

    @Scheduled(cron="0 */10 * * * ?")
//    @Scheduled(cron="0 */2 * * * ?")
    public void sendSQEmail() {

        try {
            LOG.info("==============售中人天填写预警邮件发送===============");
            Date date = new Date();
            iMail.sendSQEmail();
            LOG.info("售中人天填写预警邮件发送成功："+date);
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e );
                emailUtil.sendMailMe("售中人天填写预警---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("售中人天填写预警邮件报错" + e.toString());
                LOG.info("售中人天填写预警邮件报错信息是：" + e + "时间：" + date);
            }
        }

    }

    /**
     * 新建超8项目
     */
//    @Scheduled(cron="0 */2 * * * ?")
    @Scheduled(cron="0 10 20 ? * ?")
    public void createSuperProduct() {
        try {
            LOG.info("==============新建超8项目===============");
            pushData.createSuperModel();
            LOG.info("======================================================");
        } catch (Exception e) {
            if(e.toString().equals("java.lang.NullPointerException")){
                e.printStackTrace();
                LOG.info("运行错误信息是：" + e  );
                emailUtil.sendMailMe("新建超8项目---空指针异常" );
            }else {
                e.printStackTrace();
                Date date = new Date();
                emailUtil.sendMail("新建超8项目---" + e.toString());
                LOG.info("运行错误信息是：" + e + "时间：" + date);
            }
        }

    }
    //每三小时
//    @Scheduled(cron="0 */2 * * * ?")
//    @Scheduled(cron="0 0 0/3 * * ?")
//    public void UpdateCrmInstallData() {
//        try {
//            LOG.info("==============向crm推送实施数据===============");
//            pushData.UpdateCrmInstallData();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            Date date = new Date();
//            emailUtil.sendMail("向crm推送数据报错---"+e.toString());
//            LOG.info("运行错误信息是："+e+"时间："+date);
//        }
//
//    }


//    @Scheduled(cron="0 */2 * * * ?")
//    public void updateWorkTime() throws Exception{
//        LOG.info("==============修改售后工时===============");
//        Date date = new Date();
//        iGetAfterSales.getWorkTime();
//        LOG.info("获取售后工时完成："+date);
//    }

}
